package com.fr.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 日期格式化工具类
 * 
 * @author qinyu
 */
public class DateFormatUtils {

	// Suppress default constructor for noninstantiability
	private DateFormatUtils() {
		throw new AssertionError();
	}

	public enum DateFormats {
		/**
		 * yyyyMMdd
		 */
		DATE_BASIC("yyyyMMdd"),
		/**
		 * yyyy-MM-dd
		 */
		DATE_STANDARD("yyyy-MM-dd"),
		/**
		 * yyyy.MM.dd
		 */
		DATE_DOT("yyyy.MM.dd"),
		/**
		 * yyyy-MM-dd HH:mm:ss
		 */
		DATE_TIME_STANDARD("yyyy-MM-dd HH:mm:ss"),
		/**
		 * yyyy-MM-dd HH:mm:ss
		 */
		DATE_TIME_INDEX_SUFFIX("_yyyyMMdd_HHmm");

		private SimpleDateFormat formatter;

		DateFormats(String format) {
			this.formatter = new SimpleDateFormat(format);
		}

		/**
		 * 按指定的格式返回格式化后的日期字符串
		 */
		public  String format(Date date) {
			return formatter.format(date);
		}

		/**
		 * 按指定的格式返回日期字符串对应的日期
		 */
		public  Date parse(String dateStr) throws ParseException {
			return formatter.parse(dateStr);
		}
	}

	/**
	 * 按指定的格式返回格式化后的日期字符串
	 */
	public static String format(Date date, DateFormats format) {
		return format.format(date);
	}

	/**
	 * 按指定的格式返回日期字符串对应的日期
	 */
	public static Date parse(String dateStr, DateFormats format) throws ParseException {
		return format.parse(dateStr);
	}

	/**
	 * 按"_yyyyMMdd_HHmm"格式返回当前时间后缀
	 */
	public static String getTimeSuffix() {
		return DateFormats.DATE_TIME_INDEX_SUFFIX.format(Calendar.getInstance().getTime());
	}

	/**
	 * 将 YYYYMMDD 格式的字符串转换为 YYYY.MM.DD 格式
	 */
	public static String formatDateWithDot(Object obj) {
		if (obj == null) {
			return "";
		}
		String date = String.valueOf(obj);
		if (date.length() == 8) {
			return date.substring(0, 4) + "." + date.substring(4, 6) + "." + date.substring(6, 8);
		} else {
			return "";
		}
	}

	/**
	 * 检查给定日期距离当前时间是否超过（或等于）指定范围
	 */
	public static boolean checkDateDiff(Date date, long diff, TimeUnit unit) {
		return checkDateDiff(new Date(), date, diff, unit);
	}

	/**
	 * 检查给定的两个日期的时间差是否超过（或等于）指定范围
	 */
	public static boolean checkDateDiff(Date date1, Date date2, long diff, TimeUnit unit) {
		long timeDiff = date1.getTime() - date2.getTime();

		switch (unit) {
		case DAYS:
			timeDiff = TimeUnit.MILLISECONDS.toDays(timeDiff);
			break;
		case HOURS:
			timeDiff = TimeUnit.MILLISECONDS.toHours(timeDiff);
			break;
		case MINUTES:
			timeDiff = TimeUnit.MILLISECONDS.toMinutes(timeDiff);
			break;
		case SECONDS:
			timeDiff = TimeUnit.MILLISECONDS.toSeconds(timeDiff);
			break;
		case MICROSECONDS:
			timeDiff = TimeUnit.MILLISECONDS.toMicros(timeDiff);
			break;
		case NANOSECONDS:
			timeDiff = TimeUnit.MILLISECONDS.toNanos(timeDiff);
			break;
		default:
			break;
		}

		if (Math.abs(timeDiff) >= diff) {
			return true;
		} else {
			return false;
		}
	}
}